﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;


using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;

using BL.Client.BudgetManageBL;




namespace UI.BudgetManage
{
    public partial class BMUserLogin : Form
    {
        BudgetManageServerice BMS;
        WaitForm.WaitForm WF;
        Thread LoginThread;

        string ServerIP;
        string UserName;
        string UserPassword;

        delegate void UserIndentify(OperateResult dr);
        event UserIndentify OnUserIndentify;

        public BMUserLogin()
        {
            InitializeComponent();
         //   this.skinUI1.SkinFile = System.IO.Directory.GetCurrentDirectory() + "\\skins\\Watercolor-ERGONOMIC.skn";
         }

        private void BMUserLogin_Load(object sender, EventArgs e)
        {
            this.BMS = new BudgetManageServerice("BudgetManage.ini");
            System.Runtime.Remoting.Channels.ChannelServices.RegisterChannel(new TcpClientChannel(), false);
            this.OnUserIndentify += new UserIndentify(this.Login);
        }

        private void BMUserLogin_FormClosing(object sender, FormClosingEventArgs e)
        {
            foreach (IChannel ic in ChannelServices.RegisteredChannels)
            {
                ChannelServices.UnregisterChannel(ic);
            }
            if (this.LoginThread.IsAlive)
            {
                this.LoginThread.Abort();
            }
        }

        private void sbtnQuit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            this.ServerIP = cbbServerIP.Text;
            this.UserName = cbbUserName.Text;
            this.UserPassword = tbPassword.Text;

            WF = new UI.BudgetManage.WaitForm.WaitForm("正在登录中");
            WF.StartPosition = this.StartPosition;
            WF.AddProgress(20);
            this.LoginThread = new Thread(new ThreadStart(this.Indentify));
            LoginThread.Start();
            this.Hide();

            DialogResult dr = WF.ShowDialog();
            if (dr == DialogResult.No)
            {
                this.Show();
                this.Focus();
                this.LoginThread.Abort();
            }
        }

        private void Indentify()
        {
            string server_ip = this.ServerIP;
            string user_name = this.UserName;;
            string user_password = this.UserPassword;
            if (user_name == "" || user_password == "")
            {
                MessageBox.Show("请正确填写信息！");
            }
            OperateResult result = BMS.UserIndentify(server_ip, user_name, user_password);
            this.BeginInvoke(OnUserIndentify, result);
        }

        private void Login(OperateResult dr)
        {
            WF.AddProgress(30);
            if (dr == OperateResult.Success)
            {
                WF.AddProgress(30);
                BMMain bmmain = new BMMain(this, this.BMS);
                WF.AddProgress(20);
                this.Visible = false;
                bmmain.Show();
            }
            else
            {
                if (dr == OperateResult.Fail)
                {
                    MessageBox.Show("用户名不存在或密码错误！");
                }
                else if (dr == OperateResult.DisConnect)
                {
                    MessageBox.Show("请检查网络链接");
                }
                this.Show();
                this.Focus();
            }
            WF.Close();
        }



      


    }
}
